From 59091f2d52524fa9157428f636023a274c2389e6 Mon Sep 17 00:00:00 2001 From: Keir Fraser Date: Sat, 24 Nov 2007 13:26:39 +0000 Subject: [PATCH] [Mini-OS] Make semaphores callback-safe One may want to use semaphores in event handlers to wake threads waiting for a resource, so semaphores then need to be callback-safe. Signed-off-by: Samuel Thibault --- extras/mini-os/include/semaphore.h | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/extras/mini-os/include/semaphore.h b/extras/mini-os/include/semaphore.h index 47365d25fd..2c6394265e 100644 --- a/extras/mini-os/include/semaphore.h +++ b/extras/mini-os/include/semaphore.h @@ -49,14 +49,25 @@ static inline void init_MUTEX(struct semaphore *sem) static void inline down(struct semaphore *sem) { - wait_event(sem->wait, sem->count > 0); + unsigned long flags; + while (1) { + wait_event(sem->wait, sem->count > 0); + local_irq_save(flags); + if (sem->count > 0) + break; + local_irq_restore(flags); + } sem->count--; + local_irq_restore(flags); } static void inline up(struct semaphore *sem) { + unsigned long flags; + local_irq_save(flags); sem->count++; wake_up(&sem->wait); + local_irq_restore(flags); } /* FIXME! Thre read/write semaphores are unimplemented! */ -- 2.30.2